#include <iostream>
#include <unistd.h>
#include "Ringqueue.hpp"
#include "task.hpp"
#include <string>
using namespace std;

string opes = "+-*/%";

void *Ring_C(void *args) // 消费
{
    Ringqueue<Task> *bp = static_cast<Task> * (args);

    while (true)
    {
        Task t;
        bp->pop(&t);

        t();
        cout << "Ring_C,处理完成任务: " << t.Print_res() << endl;
    }
}

void *Ring_P(void *args)
{

    Ringqueue<Task> *bp = static_cast<Task> * (args);
    while (true)
    {
        int x = rand() % 100;
        int y = rand() % 10;
        char op = opes[rand() % opes.size()];

        Task t(x, y, op);
        bp->push(t);

        cout << "Ring_P,生产任务：" << t.Print_oper() << endl;
    }
}

int main()
{
    srand(time(nullptr) ^ getpid());

    Ringqueue<Task> *bp = new Ringqueue<Task>();

    // 单消费单生产模式
    pthread_t c, p;

    pthread_create(&c, nullptr, Ring_C, bp); // 消费
    pthread_create(&p, nullptr, Ring_P, bp); // 生产

    pthread_join(&c);
    pthread_join(&p);

    return 0;
}